package com.card.hwatt.controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.UUID;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.hwatt.assembler.*;
import com.card.hwatt.service.*;
import com.card.hwatt.core.RequestJsonParam;
import com.card.hwatt.dto.*;
import com.card.hwatt.entity.NightCheck;

/**
 * 时间段
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/worktime")
public class WorktimeController extends BaseController{

	@Autowired
	@Qualifier("workTimeService")
	private WorkTimeService workTimeService;

	@Autowired
	@Qualifier("scheduleService")
	private ScheduleService scheduleService;

	@RequestMapping("/index")
	public String index() {
		return "worktime";
	}

	@RequestMapping("/save")
	public @ResponseBody
	ReturnValue save(@RequestJsonParam WorkTime worktime) {

		if (worktime.id == null) {
			worktime.id = UUID.randomUUID();
			workTimeService.save(WorkTimeAssembler.convert(worktime));
		} else {
			workTimeService.update(WorkTimeAssembler.convert(worktime));
		}

		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}

	@RequestMapping("/delete")
	public @ResponseBody
	ReturnValue delete(@RequestParam UUID id) {
		workTimeService.delete(id);
		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}

	@RequestMapping("/loadAll")
	public @ResponseBody
	List<WorkTime> loadAll() {
		List<WorkTime> result = WorkTimeAssembler.convert(workTimeService
				.loadAll(new PageParam(0, 0, "name")).rows);
		return result;
	}

	@RequestMapping("/loadAllCurrent")
	public @ResponseBody
	List<WorkTime> loadAllCurrent(Long id) {
		List<WorkTime> result = new ArrayList<WorkTime>();
		if (id == null) {
			return result;
		}

		Calendar calendar = Calendar.getInstance();
		com.card.hwatt.entity.Schedule schedule = scheduleService.get(
				String.format("%d-%d-01", calendar.get(Calendar.YEAR),
						calendar.get(Calendar.MONTH) + 1), id);

		if (schedule == null) {
			return result;
		}

		result.add(WorkTimeAssembler.convert(schedule.morning));
		result.add(WorkTimeAssembler.convert(schedule.afternoon));

		if (schedule.night != null) {
			result.add(WorkTimeAssembler.convert(schedule.night));

			if (CollectionUtils.isNotEmpty(schedule.nightCheckList)) {

				Collections.sort(schedule.nightCheckList,
						new Comparator<NightCheck>() {
							@Override
							public int compare(NightCheck o1, NightCheck o2) {
								return Integer.valueOf(o1.orderBy).compareTo(
										Integer.valueOf(o2.orderBy));
							}
						});

				for (NightCheck nightCheck : schedule.nightCheckList) {
					WorkTime time = new WorkTime();
					result.add(time);
					time.name = "第"
							+ (schedule.nightCheckList.indexOf(nightCheck) + 1)
							+ "巡检";

					if (nightCheck.startTime != null) {
						calendar.set(2000, 0, 1, Integer
								.valueOf(nightCheck.startTime.substring(0, 2)),
								Integer.valueOf(nightCheck.startTime.substring(
										2, 4)), 0);

						time.startBestTime = DateFormatUtils.format(calendar,
								"HH:mm");
						calendar.add(Calendar.SECOND, nightCheck.endTime * 60);
						time.endBestTime = DateFormatUtils.format(calendar,
								"HH:mm");
					}
				}
			}
		}

		return result;
	}
}
